//
//  YMTabBarButton.m
//  YiMiao
//
//  Created by apple on 15/11/27.
//  Copyright © 2015年 yimiao.tm. All rights reserved.
//

#import "YMTabBarButton.h"
#import "ym_OderButton.h"
#import "GRToolKit.h"
#define GRTabBarButtonRoatio 1.2


@interface YMTabBarButton()
@property (nonatomic, weak)ym_OderButton *badgeButton;
@end
@implementation YMTabBarButton
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.imageView.contentMode = UIViewContentModeCenter;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.font = [UIFont systemFontOfSize:12];
        [self setTitleColor:[UIColor colorWithRed:0.27f green:0.27f blue:0.27f alpha:1.00f] forState:UIControlStateNormal];
        [self setTitleColor:[UIColor colorWithRed:0.737 green:0.000 blue:0.031 alpha:1.000] forState:UIControlStateSelected];
        
        
        // 初始化badgeValue
        ym_OderButton *badgeValue = [[ym_OderButton alloc]init];
        self.badgeButton = badgeValue;
        badgeValue.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin;
        [self addSubview:badgeValue];
        
    }
    
    return self;
}



// 重写image的frame的方法
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    CGFloat imageW = self.frame.size.width;
    CGFloat imageH = self.frame.size.height * GRTabBarButtonRoatio;
    return  CGRectMake(0, 0, imageW, imageH);
}
// 重写title的frame的方法
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGFloat titleW = self.frame.size.width;
    CGFloat titleY = self.frame.size.height * GRTabBarButtonRoatio+10;
    CGFloat titleH = self.frame.size.height - titleY;
    return  CGRectMake(0, titleY, titleW, titleH);
}
- (void)setItem:(UITabBarItem *)item
{
    _item = item;
    
    [self observeValueForKeyPath:nil ofObject:nil change:nil context:nil];
    
    // 对item的属性进行监听
    [self.item addObserver:self forKeyPath:@"badgeValue" options:NSKeyValueObservingOptionNew context:nil];
    [self.item addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
    [self.item addObserver:self forKeyPath:@"image" options:NSKeyValueObservingOptionNew context:nil];
    [self.item addObserver:self forKeyPath:@"selectedImage" options:NSKeyValueObservingOptionNew context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    // 设置title
    [self setTitle:self.item.title forState:UIControlStateNormal];
    [self setTitle:self.item.title forState:UIControlStateSelected];
    
    // 设置image
    [self setImage:self.item.image forState:UIControlStateNormal];
    [self setImage:self.item.selectedImage forState:UIControlStateSelected];
    
    // 根据字符串的内容设置badgeValue的大小
    self.badgeButton.badgeValue = self.item.badgeValue;
    CGFloat badgeButtonX;
    CGFloat badgeButtonY;
    if (self.badgeButton.badgeValue.length>1) {
        badgeButtonX = self.frame.size.width - self.badgeButton.frame.size.width+5;
//        badgeButtonY = 8;
    }else{
        badgeButtonX = self.frame.size.width - self.badgeButton.frame.size.width+5;
    }
    
    badgeButtonY =-2;
    // 设置badgeButton的frame
    CGRect frame = self.badgeButton.frame;
    frame.origin.x = badgeButtonX;
    frame.origin.y = badgeButtonY;
    
    self.badgeButton.frame = frame;
}
-(void)dealloc
{
    [self.item removeObserver:self forKeyPath:@"badgeValue"];
    [self.item removeObserver:self forKeyPath:@"title"];
    [self.item removeObserver:self forKeyPath:@"image"];
    [self.item removeObserver:self forKeyPath:@"selectedImage"];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
